# -*- coding: utf-8 -*-
from functools import partial

from sqlalchemy.exc import IntegrityError

from common.lottery.cyclical.abstract import activity as base
from common.lottery.cyclical.cq_ssc.db.activity import calc_miss
from common.utils.decorator import sql_wrapper
from common.lottery import LOTTERY_TYPE


fullfill_result = sql_wrapper(
    partial(base.fullfill_result, LOTTERY_TYPE.TJ_SSC))


get_latest_term = sql_wrapper(
    partial(base.get_latest_term, LOTTERY_TYPE.TJ_SSC))


get_activity_by_term = sql_wrapper(
    partial(base.get_activity_by_term, LOTTERY_TYPE.TJ_SSC))


stop_term = sql_wrapper(
    partial(base.stop_term, LOTTERY_TYPE.TJ_SSC))


create_new_term = sql_wrapper(
    partial(base.create_new_term, LOTTERY_TYPE.TJ_SSC))


get_stats_by_term = sql_wrapper(
    partial(base.get_stats_by_term, LOTTERY_TYPE.TJ_SSC))


get_activity_stats = sql_wrapper(
    partial(base.get_activity_stats, LOTTERY_TYPE.TJ_SSC))


@sql_wrapper
def insert_stats(term, number, last_stats):
    trend, save = base.generate_trend(
        LOTTERY_TYPE.TJ_SSC, term, number, last_stats)
    trend, save = calc_miss(trend, save, number, last_stats)
    if save:
        try:
            trend.save()
        except IntegrityError:
            pass
    return trend
